package com.kgc.manger.service.impl;

import com.kgc.dao.TbContentCategoryMapper;
import com.kgc.entity.TbContentCategory;
import com.kgc.entity.TbContentCategoryExample;
import com.kgc.manger.service.ContentCatService;
import com.kgc.utils.ItripResult;
import com.kgc.utils.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ContentCatServiceImpl implements ContentCatService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Override
    public List<TbContentCategory> findAll() {
        TbContentCategoryExample example=new TbContentCategoryExample();
        return tbContentCategoryMapper.selectByExample(example);
    }

    @Override
    public List<TreeNode> listCat() {
        return tbContentCategoryMapper.listCat();
    }

    @Override
    public ItripResult addContentCategory(Long pid, String name) {
        TbContentCategory contentCategory=new TbContentCategory();
        contentCategory.setName(name);
        contentCategory.setParentId(pid);
        contentCategory.setSortOrder(1);
        contentCategory.setStatus(1);
        contentCategory.setIsParent(false);
        contentCategory.setCreated(new Date());
        contentCategory.setUpdated(new Date());
        tbContentCategoryMapper.insertSelective(contentCategory);
        return ItripResult.ok();//status 200 msg ok  datanull
    }

    @Override
    public ItripResult updateContentCategory(Long id, String name) {
        TbContentCategory contentCategory=new TbContentCategory();
        contentCategory.setId(id);
        contentCategory.setName(name);
        contentCategory.setUpdated(new Date());
        tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        return ItripResult.build(200,"修改成功!");
    }

    @Override
    public ItripResult deleteContentCategory(Long id) {

        tbContentCategoryMapper.deleteByPrimaryKey(id);
        return ItripResult.build(200,"删除成功!");
    }
}
